package com.iflyun.rabbitmqcustmer.litener;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.iflyun.rabbitmqcustmer.domain.WeChatLog;
import com.iflyun.rabbitmqcustmer.service.WeChatProducerService;

@Controller
public class WechatLitener implements  MessageListener {

	
	private static final Logger logger = Logger.getLogger(WebLogLitener.class);
	
	private List<WeChatLog> list =new ArrayList<WeChatLog>();
	
	@Resource(name = "weChatProducerService")
	private WeChatProducerService weChatProducerService;
	
	@Override
	public void onMessage(Message message) {
		String json="";
		try {
			json=new String(message.getBody(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("json转换失败", e);
		}
		WeChatLog wechat = JSON.parseObject(json, WeChatLog.class); 
		
		try {
			list.add(wechat);
			if(list.size()>=1000){
				weChatProducerService.setWeChatList(list);
				list.clear();
			}
		} catch (Exception e1) {
			logger.error("处理调用接口记录信息发生错误：",e1);
		}
		
	}

}
